﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SdmxMl.Excel;


namespace SasPlugIn.Table
{
    public partial class ucAnnotationSel : UserControl
    {
        public AnnotationInventory AnInventory { get; private set; }

        public ucAnnotationSel()
        {
            InitializeComponent();
        }

        #region Annotation Grid Events

        /// <summary>
        /// Select / Unselect All in annotations list
        /// </summary>
        /// <param name="sender"></param> <param name="e"></param>
        private void dataGridViewAnnot_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                DataTable tb = dataGridViewAnnot.DataSource as DataTable;
                if (tb != null && tb.Rows.Count > 0)
                {
                    bool selectedFlag = !(Convert.ToBoolean(tb.Rows[0][0]));

                    foreach (DataRow r in tb.Rows)
                        r[0] = selectedFlag;

                    tb.AcceptChanges();
                }
            }
        }

        /// <summary>
        /// Reset previous content for SAS table operation
        /// </summary>
        public void ClearAnnotationGrid()
        {
            DataTable tb = dataGridViewAnnot.DataSource as DataTable;
            if (tb != null)
            {
                tb.Rows.Clear();
                tb.AcceptChanges();
            }
        }

        /// <summary> Fetchs selection of annotations as table </summary>
        /// <returns>a DataTable</returns>
        public DataTable GetAnnotationsSelectionTable()
        {
            dataGridViewAnnot.EndEdit();
            DataTable tb = dataGridViewAnnot.DataSource as DataTable;
            if (tb != null)
                tb.AcceptChanges();

            return tb;

        }

        /// <summary>
        /// Provides annotailons list informations
        /// </summary>
        /// <param name="anInventory">Annotation Inventory instance</param>
        public void SetInventoryDataSource(AnnotationInventory inventory)
        {
            AnInventory = inventory;

            if (AnInventory != null)
                dataGridViewAnnot.DataSource = AnInventory.TableInfoSelection;
        }


        public string GetSelectedAnnot()
        {
            dataGridViewAnnot.EndEdit();
            DataTable tb = GetAnnotationsSelectionTable();
            StringBuilder sb = new StringBuilder();
            if (tb != null)
            {
                foreach (DataRow row in tb.Rows)
                    if ((bool)row["Sel"] == true)
                    {
                        if (sb.Length > 0)
                            sb.Append(';');
                        sb.Append(row["Ann"].ToString());
                    }
            }
            return sb.ToString();
        }
        #endregion

    }
}
